﻿//------------------------------------------------------------------
//
//  For licensing information and to get the latest version go to:
//  http://www.codeplex.com/perspective
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY
//  OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
//  LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
//  FITNESS FOR A PARTICULAR PURPOSE.
//
//------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using Perspective.Wpf3D.Primitives;
using Perspective.Wpf3D.Sculptors;

namespace Perspective.Wpf3D.Shapes
{
    /// <summary>
    /// 棱柱型
    /// A 3D prism element.
    /// By default, the direction of the prism is the Z axis, and the length is 1.0.
    /// Default radius is 1.0.
    /// </summary>
    public class Prism3D : PolygonalElement3D
    {
        #region Constructors

        /// <summary>
        /// 棱柱或圆柱，圆柱的SideCount较大
        /// Static constructor.
        /// No default back material for 3D closed shapes.
        /// </summary>
        static Prism3D()
        {
            BackMaterialProperty.OverrideMetadata(
                typeof(Prism3D),
                new PropertyMetadata(
                    null,
                    VisualPropertyChanged));
        }

        #endregion


        #region Properties

        #region CircumRadius 外接圆半径

        /// <summary>
        /// 外接圆半径
        /// Gets or sets the polygon circumcircle radius.
        /// Default value is 10.0.
        /// </summary>
        public double CircumRadius
        {
            get { return (double)GetValue(CircumRadiusProperty); }
            set { SetValue(CircumRadiusProperty, value); }
        }

        /// <summary>
        /// Identifies the circumcircle radius dependency property.
        /// </summary>
        public static readonly DependencyProperty CircumRadiusProperty =
            DependencyProperty.Register(
                "CircumRadius",
                typeof(double),
                typeof(Prism3D),
                new UIPropertyMetadata(
                    1.0,
                    VisualPropertyChanged),
                    PositiveDoubleValidateValue);
        #endregion

        #region Height 高度

        /// <summary>
        /// 高度
        /// Gets or sets the model side count.
        /// Default value is 1.0. Minimum value is 0.01.
        /// </summary>
        public double Height
        {
            get { return (double)GetValue(HeightProperty); }
            set { SetValue(HeightProperty, value); }
        }

        /// <summary>
        /// Identifies the Height dependency property.
        /// </summary>
        public static readonly DependencyProperty HeightProperty =
            DependencyProperty.Register(
                "Height",
                typeof(double),
                typeof(Prism3D),
                new PropertyMetadata(
                    1.0,
                    VisualPropertyChanged),
                    HeightValidateValue);

        /// <summary>
        /// Validation of the Height value.
        /// </summary>
        /// <param name="value">Value to test.</param>
        /// <returns>Boolean value.</returns>
        public static bool HeightValidateValue(object value)
        {
            double i = (double)value;
            return i >= 0.001;
        }

        #endregion

        #region TexturePosition 纹理坐标位置

        /// <summary>
        /// 纹理坐标位置
        /// Gets or sets the position a a texture. Default is OnSides.
        /// </summary>
        public TexturePositions TexturePosition
        {
            get { return (TexturePositions)GetValue(TexturePositionProperty); }
            set { SetValue(TexturePositionProperty, value); }
        }

        /// <summary>
        /// Identifies the TexturePosition dependency property.
        /// </summary>
        public static readonly DependencyProperty TexturePositionProperty =
            DependencyProperty.Register(
            "TexturePosition", 
            typeof(TexturePositions), 
            typeof(Prism3D), 
            new PropertyMetadata(TexturePositions.OnSides));

        #endregion
        
        #endregion

        /// <summary>
        /// Called by UIElement3D.InvalidateModel() to update the 3D model.
        /// </summary>
        protected override void OnUpdateModel()
        {
            Sculptor = new PrismSculptor(CircumRadius,Height,SideCount, InitialAngle, RoundingRate);
            (Sculptor as PrismSculptor).TexturePosition = this.TexturePosition;
            // _sculptor.Initialize(SideCount, InitialAngle, RoundingRate);
            //_sculptor.BuildMesh();
            //Geometry = _sculptor.Mesh;
            base.OnUpdateModel();
        }
    }
}
